package com.qc.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qc.servicebase.config.GuliException;
import com.qc.serviceedu.entity.EduCourse;
import com.qc.serviceedu.entity.EduCourseDescription;
import com.qc.serviceedu.entity.EduVideo;
import com.qc.serviceedu.entity.vo.CourseInfoVo;
import com.qc.serviceedu.entity.vo.CoursePublicVo;
import com.qc.serviceedu.mapper.EduCourseMapper;
import com.qc.serviceedu.query.CourseQuery;
import com.qc.serviceedu.service.EduChapterService;
import com.qc.serviceedu.service.EduCourseDescriptionService;
import com.qc.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * <p>
 * 课程 服务实现类
 * </p>
 * @author AltmanQc
 * @since 2020-12-25
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    //注入小节和章节的service
    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    //添加课程的基本信息方法
    @Override
    public String saveCourse(CourseInfoVo courseInfoVo) {
        //1.向课程表添加课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = this.baseMapper.insert(eduCourse);
        if (i == 0){
            //添加失败
            throw new GuliException(20001,"添加课程信息失败");
        }
        //获取添加之后的课程id
        String courseId = eduCourse.getId();
        //2.向课程简介表添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(courseId);
        courseDescriptionService.save(eduCourseDescription);

        return courseId;
    }

    //根据课程id查询课程的基本信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //根据课程表查询
        EduCourse course = this.getById(courseId);
        if(course == null){
            throw new GuliException(20001, "数据不存在");
        }
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(course, courseInfoVo);
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        BeanUtils.copyProperties(courseDescription,courseInfoVo);
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfoVo(CourseInfoVo courseInfoVo) {
        //先去修改课程表
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,course);
        int i = this.baseMapper.updateById(course);
        if (i==0){
            throw new GuliException(20001,"修改课程信息失败");
        }
        //再去修改描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        courseDescriptionService.updateById(courseDescription);
    }

    @Override
    public CoursePublicVo publishCourseInfo(String courseId) {
        //调用mapper
        CoursePublicVo coursePublicVo = baseMapper.getCoursePublicVo(courseId);
        return coursePublicVo;
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(teacherId) ) {
            queryWrapper.eq("teacher_id", teacherId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.ge("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.le("subject_id", subjectId);
        }
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public void deleteCourse(String courseId) {
        //1.根据课程id删除小节
        eduVideoService.removeVideoByCourseId(courseId);
        //2.根据课程id删除章节
        eduChapterService.removeChapterByCourseId(courseId);
        //3.根据课程id删除描述
        courseDescriptionService.removeById(courseId);
        //4.删除课程
        int i = this.baseMapper.deleteById(courseId);
        if (i == 0){
            throw new GuliException(20001,"删除课程异常");
        }
    }

    @Cacheable(value = "course", key = "'CourseList'")
    @Override
    public List<EduCourse> CourseList() {
        //查询前8热门课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        List<EduCourse> courseList = baseMapper.selectList(wrapper);
        return courseList;
    }


}
